package com.wanfajie.smsdistribute.util

import android.app.AlertDialog
import android.content.Context
import android.content.DialogInterface
import android.text.Spannable
import android.text.SpannableString
import android.text.style.CharacterStyle
import android.widget.Toast
import com.wanfajie.smsdistribute.R

private lateinit var toastInstance : Toast

fun refreshToast(ctx : Context) {
    if (::toastInstance.isInitialized) toastInstance.cancel()
    toastInstance = Toast.makeText(ctx, "", Toast.LENGTH_SHORT)
}

fun toast(message : String = "", strId : Int = 0, ctx : Context? = null) {
    if (strId == 0 && message.isEmpty()) {
        throw IllegalArgumentException("未指定消息字符串或消息ID")
    }

    if (strId != 0 && !message.isEmpty()) {
        throw IllegalArgumentException("同时指定消息字符串和消息ID")
    }

    ctx?.let {
        refreshToast(ctx)
    }

    when {
        !message.isEmpty() -> toastInstance.setText(message)
        strId != 0 -> toastInstance.setText(strId)
    }

    toastInstance.show()
}

fun confirm(
        ctx : Context,
        title: Int,
        message: Int,
        okId: Int = R.string.ok,
        cancelId: Int = R.string.cancel,
        otherId: Int = 0,
        callback: (DialogInterface, Int) -> Unit
) {
    val cbInterface = DialogInterface.OnClickListener(callback)
    confirm(ctx, title, message, cbInterface, cbInterface, cbInterface, okId, cancelId, otherId)
}

fun confirm(
        ctx : Context,
        title: Int,
        message: String,
        okId: Int = R.string.ok,
        cancelId: Int = R.string.cancel,
        otherId: Int = 0,
        callback: (DialogInterface, Int) -> Unit
) {
    val cbInterface = DialogInterface.OnClickListener(callback)
    confirm(ctx, title, message, cbInterface, cbInterface, cbInterface, okId, cancelId, otherId)
}

fun confirm(
        ctx : Context,
        title: Int,
        message: Int,
        okCallback: DialogInterface.OnClickListener,
        cancelCallback: DialogInterface.OnClickListener,
        otherCallback: DialogInterface.OnClickListener? = null,
        okId: Int = R.string.ok,
        cancelId: Int = R.string.cancel,
        otherId: Int = 0,
        cancelable: Boolean = true
) {
    AlertDialog.Builder(ctx).apply {
        setTitle(title)
        setMessage(message)
        setCancelable(cancelable)
        setPositiveButton(okId, okCallback)
        setNegativeButton(cancelId, cancelCallback)
        if (otherId > 0) setNeutralButton(otherId, otherCallback)
    }.show()
}

fun confirm(
        ctx : Context,
        title: Int,
        message: String,
        okCallback: DialogInterface.OnClickListener,
        cancelCallback: DialogInterface.OnClickListener,
        otherCallback: DialogInterface.OnClickListener? = null,
        okId: Int = R.string.ok,
        cancelId: Int = R.string.cancel,
        otherId: Int = 0,
        cancelable: Boolean = true
) {
    AlertDialog.Builder(ctx).apply {
        setTitle(title)
        setMessage(message)
        setCancelable(cancelable)
        setPositiveButton(okId, okCallback)
        setNegativeButton(cancelId, cancelCallback)
        if (otherId > 0) setNeutralButton(otherId, otherCallback)
    }.show()
}

fun highLight(str: String, kw: String, styleFactory: () -> CharacterStyle): Spannable {
    val spanStr = SpannableString(str)
    if (kw.isEmpty()) return spanStr

    val keywordLen = kw.length

    var start = str.indexOf(kw, ignoreCase = true)
    while (start > -1) {
        val end = start + keywordLen
        val style = styleFactory()
        spanStr.setSpan(style, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        start = str.indexOf(kw, end, true)
    }

    return spanStr
}
